package ren.steve.system.exception;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import ren.steve.common.result.HttpStatus;
import ren.steve.common.result.Result;


/**
 * @Author: LiuGuoJia
 * @Description:
 * @Date: Created in 2024/1/18 18:55
 */

@ControllerAdvice
public class GlobalExceptionHandler {

  // 全局异常处理
  @ExceptionHandler(Exception.class)
  @ResponseBody
  public Result error(Exception e) {
    // return Result.fail().message("全局异常处理!");
    e.printStackTrace();
    return Result.fail().message("全局 >>> " + e.toString());
  }

  // 特定异常处理
  @ExceptionHandler(ArithmeticException.class)
  @ResponseBody
  public Result error (ArithmeticException e) {
    e.printStackTrace();
    return Result.fail().message("特定异常处理!");
  }

  // 自定义异常处理 Custom
// 特定异常处理
  @ExceptionHandler(CustomException.class)
  @ResponseBody
  public Result error (CustomException e) {
    e.printStackTrace();
    return Result.fail().code(e.getCode()).message(e.getMessage());
  }

  @ExceptionHandler(AccessDeniedException.class)
  @ResponseBody
  public Result error(AccessDeniedException error) throws AccessDeniedException {
    error.printStackTrace();
    return Result.build(null, HttpStatus.UNAUTHORIZED);
  }

}
